#!/bin/sh

. /etc/default/charon

killpid () {
	PIDFILE="/var/run/$1.pid"
	[ -f $PIDFILE ] && kill $(cat $PIDFILE) && rm $PIDFILE
}

if [ "$CHARON_ENABLE" = "yes" ]; then
	(brctl show | grep "$WL" > /dev/null) && brctl delif br0 "$WL"
	ifconfig ${WL} ${CHARON_NET}.1 netmask 255.255.255.0 up
	OLDEH=$(grep -v rcpt.to /etc/hosts)
	NEWEH="$OLDEH
${CHARON_NET}.1	charon.rcpt.to
${CHARON_FF}	firefox.rcpt.to"
	[ "$(cat /etc/hosts)" = "$NEWEH" ] || ( echo "Replacing /etc/hosts with '$NEWEH'" ; echo -n "$NEWEH" > /etc/hosts )
	OLDCONF=$(cat /etc/config.xml)
	NEWCONF=$(sed -e "s/@quantum@/$CHARON_QUANTUM/g;s/@currency@/$CHARON_CURRENCY/g;s/@amount@/$CHARON_AMOUNT/g" /etc/config.xml.in)
	[ "$OLDCONF" = "$NEWCONF" ] || ( echo "Replacing /etc/config.xml" ; echo -n "$NEWCONF" > /etc/config.xml )
	killpid dnsmasq-charon
	dnsmasq -C /etc/dnsmasq-charon.conf -a ${CHARON_NET}.1 -K -F ${CHARON_NET}.2,${CHARON_NET}.254,255.255.255.0,15m
else
	(brctl show | grep "$WL" > /dev/null) || brctl addif br0 "$WL"
fi
